<?php
declare (strict_types = 1);

namespace app\common\model\admin;

use app\common\model\Base as BaseModel;
use think\model\Pivot;
/**
 * 后台用户模型
 * Class User
 */
class User extends BaseModel
{
    // 定义表名
    protected $name = 'admin_user';

    // 定义主键
    protected $pk = 'user_id';
    /**
     * 关联用户角色表表
     */
    public function role(): \think\model\relation\BelongsToMany
    {
        return $this->belongsToMany(Role::class, UserRole::class,'role_id','user_id');
    }
    public function getCreateTimeAttr($value): string
    {
        return $value?date("Y-m-d H:i:s",$value):'';
    }
    public function getUpdateTimeAttr($value): string
    {
        return $value?date("Y-m-d H:i:s",$value):'';
    }

    /**
     * 验证用户名是否重复
     * @param string $userName
     * @return mixed
     */
    public static function checkExist(string $userName): mixed
    {
        return (new static)->where('user_name', '=', $userName)
            ->where('is_delete', '=', 0)
            ->value('user_id');
    }

    /**
     * 超管用户信息
     * @param int $user_id
     * @param array $with
     * @return User|array|null
     */
    public static function detail(int $user_id,array $with=[]): User|array|null
    {
        return static::get($user_id,$with);
    }

}